# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: David Adler <d dot adler aet posteo dot de>

pkgbase=yoshimi
pkgname=(yoshimi{,-{data,docs,lv2,standalone}})
pkgver=2.3.1
pkgrel=1
pkgdesc="A sophisticated soft-synth originally forked from ZynAddSubFX"
arch=(loong64 x86_64)
url="https://yoshimi.github.io/"
_url="https://github.com/yoshimi/yoshimi"
license=(GPL-2.0-or-later)
makedepends=(
  alsa-lib
  cairo
  cmake
  fftw
  fltk
  jack
  lv2
  mxml
  ncurses
  readline
  zlib
)
checkdepends=(
  kxstudio-lv2-extensions
  lv2lint
)
# plugin exposes symbols globally if built with LTO:
# https://github.com/Yoshimi/yoshimi/issues/164
options=(!lto)
source=($pkgname-$pkgver.tar.gz::$_url/archive/refs/tags/$pkgver.tar.gz)
sha512sums=('b1a506c96debb63ed5808acf22c7be94d7f9b821d4b134c18b5f4c5749f7cb99b32f41741bc806a39871f320e90d13a87c93f601d549a628f56f502acd54d8dc')
b2sums=('4ffe13bca8d938dff1a899ad1b71e7c5e71815e3fdb09b9ab448bda1dd3ea7f2a0ef9606969c3eca0ad3be5711c4614ceb53046e61e1750639b734f8fe112c88')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

build() {
  local cmake_options=(
    -DCMAKE_INSTALL_PREFIX=/usr
    -DCMAKE_BUILD_TYPE=None
    -Wno-dev
    -B build
    -S $pkgbase-$pkgver/src
  )

  cmake "${cmake_options[@]}"
  cmake --build build --verbose
}

check() {
  cp -v build/LV2_Plugin/${pkgbase}_lv2.so $pkgbase-$pkgver/src/LV2_Plugin/
  lv2lint -Mpack -I $pkgbase-$pkgver/src/LV2_Plugin/ "http://yoshimi.sourceforge.net/lv2_plugin"
  rm -v $pkgbase-$pkgver/src/LV2_Plugin/${pkgbase}_lv2.so
  ctest --test-dir build --output-on-failure
}

package_yoshimi() {
  depends=($pkgbase-{data,docs,lv2,standalone})

  DESTDIR="$pkgdir" cmake --install build

  (
    cd "$pkgdir"
    _pick $pkgbase-data usr/share/$pkgbase/
    _pick $pkgbase-docs usr/share/doc/$pkgbase/
    _pick $pkgbase-lv2 usr/lib/lv2/
    _pick $pkgbase-standalone usr/bin/
    _pick $pkgbase-standalone usr/share/applications/
    _pick $pkgbase-standalone usr/share/icons/
    _pick $pkgbase-standalone usr/share/man/
    _pick $pkgbase-standalone usr/share/metainfo/
    _pick $pkgbase-standalone usr/share/pixmaps/
  )
}

package_yoshimi-data() {
  pkgdesc+=" - data files"

  mv -v $pkgname/* "$pkgdir"
}

package_yoshimi-docs() {
  pkgdesc+=" - documentation"

  mv -v $pkgname/* "$pkgdir"
  install -vDm 644 $pkgbase-$pkgver/{Changelog,Dependencies,README.txt,Yoshimi_Helpers} -t "$pkgdir/usr/share/doc/$pkgbase/"
}

package_yoshimi-lv2() {
  pkgdesc+=" - LV2 plugin"
  groups=(
    lv2-plugins
    pro-audio
  )
  depends=(
    cairo
    fftw
    fltk
    gcc-libs
    glibc
    lv2-host
    mxml
    yoshimi-data
    zlib
  )
  optdepends=('yoshimi-docs: for documentation')

  mv -v $pkgname/* "$pkgdir"
}

package_yoshimi-standalone() {
  pkgdesc+=" - standalone"
  groups=(pro-audio)
  depends=(
    alsa-lib
    cairo
    fftw
    fltk
    gcc-libs
    glibc
    hicolor-icon-theme
    jack
    mxml
    ncurses
    readline
    yoshimi-data
    zlib
  )
  optdepends=('yoshimi-docs: for documentation')

  mv -v $pkgname/* "$pkgdir"
}

# vim:set ts=2 sw=2 et:
